package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ArticleListener {


    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "news.down.up.topic")
    public void consume(String message){
        if (StringUtils.isBlank(message)){
            return;
        }

        Map map = JSON.parseObject(message, Map.class);
        if (map == null){
            return;
        }
        Long articleId = (Long) map.get("articleId");
        Integer enable = (Integer) map.get("enable");
        String string = enable.toString();
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(articleId);

        apArticleConfig.setIsDown("0".equals(string));

        LambdaQueryWrapper<ApArticleConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApArticleConfig::getArticleId,articleId);
        apArticleConfigMapper.update(apArticleConfig,wrapper);


    }

}
